

#找元素
def binary_search(nums,target):
    left,right = 0,len(nums) - 1
    while left <= right:
        mid = left + (right -left) // 2
        if nums[mid] == target:
            return mid
        elif nums[mid] <= target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

#找左边界
def found_left(nums,target):
    left,right = 0,len(nums) - 1 
    while left <= right:
        mid = left + (right - left) // 2
        if nums[mid] < target:
            left = mid + 1
        elif nums[mid] > target:
            right = mid - 1
        elif nums[mid] == target:
            right = mid - 1
    
    if left < 0 or left >= len(nums):
        return -1
    
    return left if nums[left] == target else -1

#找右边界
def found_right(nums,target):
    left,right = 0,len(nums) - 1
    while left <= right:
        mid = left + (right - left) // 2
        if nums[mid] < target:
            left = mid + 1
        elif nums[mid] > target:
            right = mid - 1
        elif nums[mid] == target:
            left = mid + 1
    
    if right < 0 or right >= len(nums):
        return -1
    
    return right if nums[right] == target else -1

